<script setup>
import { getDept, addDept, updateDept, listDeptTree } from "@/api/system/dept";
import { getDicts } from "@/api/system/dict/data";

const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id
  },
  rules: {
    deptCode: [{ required: true, message: "请输入部门编码", trigger: "blur" }],
    deptName: [{ required: true, message: "请输入部门名称", trigger: "blur" }]
  },
  deptTypeOptions: [],
  deptTreeList: []
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  }
  getDicts("sys_dept_type").then((response) => {
    data.value.deptTypeOptions = response.data;
  });
  listDeptTree("", 3).then((res) => {
    data.value.deptTreeList = res.data;
  });
});
// 得到节点
function getnode(node, prop, event) {
  console.log("node", node, prop, event);
  data.value.form.parentId = node.id;
}

function getInfo() {
  data.value.loading = true;
  getDept(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          addDept(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateDept(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="公司Key" prop="companyId">
            <el-input
              v-model="data.form.companyId"
              placeholder="输入公司KEY（选填）"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上级部门" prop="parentId">
            <el-tree-select
              v-model="data.form.parentId"
              :data="data.deptTreeList"
              :default-expand-all="true"
              node-key="id"
              @node-click="getnode"
              :check-strictly="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门编码" prop="deptCode">
            <el-input
              v-model="data.form.deptCode"
              placeholder="输入部门编码"
            ></el-input
          ></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门名称" prop="deptName">
            <el-input
              v-model="data.form.deptName"
              placeholder="输入部门名称"
            ></el-input
          ></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门全称" prop="deptFullName">
            <el-input
              v-model="data.form.deptFullName"
              placeholder="输入部门全称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="treeSort">
            <el-input v-model="data.form.treeSort"></el-input
          ></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="deptType">
            <el-radio-group v-model="data.form.deptType">
              <el-radio-button
                v-for="(item, index) in data.deptTypeOptions"
                :label="item.dictValue"
                >{{ item.dictLabel }}</el-radio-button
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
